package com.lu.manage.core.common.node;

import java.util.*;

public class ZTreeRightNode {


    /**
     * id : 1
     * name : 无右键菜单 1
     * open : true
     * noR : true
     * children : [{"id":11,"name":"节点 1-1","noR":true},{"id":12,"name":"节点 1-2","noR":true}]
     */
    private Integer id;
    private Integer parentId;
    private String name;
    private boolean open;
    private boolean noR;
    private Integer levels;
    private Integer sort;
    private List<ZTreeRightNode> children;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }

    public boolean isNoR() {
        return noR;
    }

    public void setNoR(boolean noR) {
        this.noR = noR;
    }

    public List<ZTreeRightNode> getChildren() {
        return children;
    }

    public void setChildren(List<ZTreeRightNode> children) {
        this.children = children;
    }

    public Integer getLevels() {
        return levels;
    }

    public void setLevels(Integer levels) {
        this.levels = levels;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public static List<ZTreeRightNode> buildNodes(List<ZTreeRightNode> nodes){
        if(nodes.size() <= 0){
            return nodes;
        }
//        Collections.sort(nodes);
        return mergeList(nodes, nodes.get(nodes.size() - 1).getLevels(), null);
    }

    public static List<ZTreeRightNode> mergeList(List<ZTreeRightNode> nodeList, int rank, Map<Integer,List<ZTreeRightNode>> listMap){
        int n;
        Map<Integer,List<ZTreeRightNode>> currentMap = new HashMap<>();
        for(n = nodeList.size() - 1; n >= 0 && nodeList.get(n).getLevels() == rank; n--){
            if(listMap != null && listMap.get(nodeList.get(n).getId()) != null){ //父亲找孩子
                nodeList.get(n).setChildren(listMap.get(nodeList.get(n).getId())); // 这里面 nodeList.id是父亲的id
            }
            if(nodeList.get(n).getParentId() != null && nodeList.get(n).getParentId() != 0){ //孩子中，亲子相认
                //判断之前节点所属的pid是否已经创建了以该pid为key的键值对，没有则根据该pid创建新的链表
                currentMap.computeIfAbsent(nodeList.get(n).getParentId(), k -> new LinkedList<>());
                //将该节点插入到对应的list头部
                currentMap.get(nodeList.get(n).getParentId()).add(0,nodeList.get(n));
            }
        }
        if(n < 0){
            return nodeList;
        } else {
            return mergeList(new ArrayList<>(nodeList.subList(0, n + 1)),nodeList.get(n).getLevels(), currentMap);
        }
    }

}
